﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ThuySan.Models
{
    public static class HtmlHelperExtension
    {
        #region Area

        public static string AreaContentPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/");
        }

        public static string AreaScriptPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/js/");
        }

        public static string AreaCssPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/css/");
        }

        public static string AreaImagesPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/images/" + module + "/");
        }

        public static string AreaScriptPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/js/" + module + "/");
        }

        public static string AreaCssPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/css/" + module + "/");
        }

        public static string AreaImagesPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/images/");
        }

        public static string AreaContentVitualPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/");
        }

        public static string AreaScriptVitualPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/js/");
        }

        public static string AreaCssVitualPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/css/");
        }

        public static string AreaImagesVitualPath(this HtmlHelper helper, string area)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/images/");
        }

        public static string AreaScriptVitualPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/js/" + module + "/");
        }

        public static string AreaCssVitualPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/css/" + module + "/");
        }

        public static string AreaImagesVitualPath(this HtmlHelper helper, string area, string module)
        {
            return VirtualPathUtility.ToAbsolute("/Areas/" + area + "/Content/images/" + module + "/");
        }

        #endregion

        #region Default

        public static string AreaContentPath(this HtmlHelper helper)
        {
            return VirtualPathUtility.ToAbsolute("/Content/");
        }

        public static string AreaScriptDefaultPath(this HtmlHelper helper)
        {
            return VirtualPathUtility.ToAbsolute("/Scripts/");
        }

        public static string AreaScriptPath(this HtmlHelper helper)
        {
            return VirtualPathUtility.ToAbsolute("/Content/js/");
        }

        public static string AreaCssPath(this HtmlHelper helper)
        {
            return VirtualPathUtility.ToAbsolute("/Content/css/");
        }

        public static string AreaImagesPath(this HtmlHelper helper)
        {
            return VirtualPathUtility.ToAbsolute("/Content/images/");
        }

        #endregion

        #region Upload Folder

        public static string UploadPath(this HtmlHelper helper)
        {
            return "/Upload/UserUpload/";
        }

        public static string UploadPath(this HtmlHelper helper, string controllerName)
        {
            return string.Format("/Upload/{0}/", controllerName);
        }

        #endregion

        public static string Truncate(this HtmlHelper helper, string values, int length)
        {
            if (string.IsNullOrEmpty(values) || values.Length <= length)
            {
                return values;
            }
            else
            {
                return string.Format("{0}...", values.Substring(0, length));
            }
        }
		
		public static string Truncate<T>(this HtmlHelper<T> helper, string values, int length)
        {
            if (string.IsNullOrEmpty(values) || values.Length <= length)
            {
                return values;
            }
            else
            {
                return string.Format("{0}...", values.Substring(0, length));
            }
        }

        #region Pager

        public static string Pager(this HtmlHelper helper, int pagePadding, int numItemPerPage, int totalRecords, string absolutePath, NameValueCollection queryString, string urlPrefix)
        {
            StringBuilder stringBuilder = new StringBuilder(absolutePath);
            stringBuilder.Append("?");
            int currentPage = 0;
            foreach (string s in queryString)
            {
                if (!s.Equals(urlPrefix, StringComparison.InvariantCultureIgnoreCase))
                {
                    stringBuilder.AppendFormat("{0}={1}&", s, queryString[s]);
                }
                else if (!int.TryParse(queryString[s], out currentPage))
                    currentPage = 0;
            }
            if (currentPage <= 0)
                currentPage = 1;

            StringBuilder sb1 = new StringBuilder();

            int seed = currentPage % pagePadding == 0
                               ? currentPage
                               : currentPage - (currentPage % pagePadding);
            if (currentPage <= 1)
                sb1.AppendFormat("<li id=\"pagination_previous\"><span class=\"disabled\">Previous</span></li>");
            else sb1.AppendFormat("<li id=\"pagination_previous\"><a href=\"{0}{1}={2}\" class=\"bordercolor\">Previous</a></li>", stringBuilder, urlPrefix, currentPage);

            if (currentPage - pagePadding >= 0)
                sb1.AppendFormat("<li><a href=\"{0}{1}={2}\" class=\"bordercolor\">...</a></li>", stringBuilder, urlPrefix, (currentPage - pagePadding) + 1);

            for (int i = seed; i < Math.Round((totalRecords / numItemPerPage) + 0.5) && i < seed + pagePadding; i++)
                sb1.AppendFormat(GetPage(currentPage, i + 1, stringBuilder, urlPrefix));

            if (currentPage + pagePadding <= (Math.Round((totalRecords / numItemPerPage) + 0.5) - 1))
                sb1.AppendFormat(GetPage(currentPage, (currentPage + pagePadding) + 1, stringBuilder, urlPrefix));

            if (currentPage < (Math.Round((totalRecords / numItemPerPage) + 0.5) - 1))
                sb1.AppendFormat("<li id=\"pagination_next\"><a href=\"{0}{1}={2}\">Next</a></li>", stringBuilder, urlPrefix, currentPage + 2);

            return sb1.ToString();
        }

        private static string GetPage(int currentPage, int page, StringBuilder stringBuilder, string urlPrefix)
        {
            if (currentPage == page)
                return string.Format("<li class=\"current\"><span>{0}</span></li>", page);
            return string.Format("<li><a href=\"{0}{1}={2}\" class=\"bordercolor\">{2}</a></li>", stringBuilder, urlPrefix, page);
        }

        #endregion

    }
}